package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.LoginMapper;
import com.itheima.mapper.SubjectMapper;
import com.itheima.mapper.TagsMapper;
import com.itheima.mapper.TokenMapper;
import com.itheima.pojo.BsUser;
import com.itheima.pojo.HmSubjects;
import com.itheima.pojo.HmSubjectsDirectorys;
import com.itheima.pojo.HmSubjectsTags;
import com.itheima.service.SubjectService;
import com.itheima.service.TagsService;
import com.itheima.util.LoginResult;
import com.itheima.util.PageResult;
import com.itheima.util.Token;
import com.itheima.util.SubjectsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TagsServiceImpl implements TagsService {
    @Autowired
    private TagsMapper tagsMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private TokenMapper tokenMapper;

    @Override
    public void add(HmSubjectsTags hmSubjectsTags) {
        hmSubjectsTags.setAdddate(new Date());
        String authorization = request.getHeader("Authorization");
        String[] s = authorization.split(" ");
        LambdaQueryWrapper<Token> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Token::getToken,s[1]);
        Token token = tokenMapper.selectOne(wrapper1);
        hmSubjectsTags.setCreatorid(token.getBsUserId());
        tagsMapper.insert(hmSubjectsTags);
    }

    @Override
    public void update(Integer id, HmSubjectsTags tags) {
        tags.setId(id);
        tagsMapper.updateById(tags);
    }

    @Override
    public PageResult list(String page, String pagesize, String subjectID, String tagName, Integer state) {
        LambdaQueryWrapper<HmSubjectsTags> wrapper = new LambdaQueryWrapper<>();
        if(subjectID!=null)
            wrapper.eq(HmSubjectsTags::getSubjectid,subjectID);
        if(tagName!=null)
            wrapper.like(HmSubjectsTags::getTagname,tagName);
        if(state!=null)
            wrapper.eq(HmSubjectsTags::getState,state);
        IPage<HmSubjectsTags> p=new Page<>(Integer.parseInt(page),Integer.parseInt(pagesize));
        IPage<HmSubjectsTags> tagsIPage = tagsMapper.selectPage(p, wrapper);
        PageResult pageResult = new PageResult();
        for (HmSubjectsTags record : tagsIPage.getRecords()) {
            BsUser bsUser = loginMapper.selectById(record.getCreatorid());
            if(bsUser!=null)
            record.setUsername(bsUser.getUsername());
            HmSubjects hmSubjects = subjectMapper.selectById(record.getSubjectid());
            if(hmSubjects!=null)
            record.setSubjectName(hmSubjects.getSubjectname());
        }
        pageResult.setItems(tagsIPage.getRecords());
        pageResult.setPagesize(tagsIPage.getSize()+"");
        pageResult.setPages(Integer.parseInt(tagsIPage.getPages()+""));
        pageResult.setCounts(Integer.parseInt(tagsIPage.getTotal()+""));
        pageResult.setPage(tagsIPage.getCurrent()+"");
        return pageResult;
    }

    @Override
    public void del(Integer id) {
        tagsMapper.deleteById(id);
    }

    @Override
    public void status(Integer id, Integer state) {
        HmSubjectsTags hmSubjectsTags = tagsMapper.selectById(id);
        hmSubjectsTags.setState(state);
        tagsMapper.updateById(hmSubjectsTags);
    }

    @Override
    public HmSubjectsTags byId(Integer id) {
        HmSubjectsTags hmSubjectsTags = tagsMapper.selectById(id);
        BsUser bsUser = loginMapper.selectById(hmSubjectsTags.getCreatorid());
        hmSubjectsTags.setUsername(bsUser.getUsername());
        return hmSubjectsTags;
    }

    @Override
    public List<SubjectsResult> findById(Integer subjectID) {
        LambdaQueryWrapper<HmSubjectsTags> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(HmSubjectsTags::getSubjectid,subjectID);
        List<HmSubjectsTags> hmSubjectsTags = tagsMapper.selectList(wrapper);
        //创建返回值对象
        List<SubjectsResult> subjectsResults = new ArrayList<>();
        for (HmSubjectsTags hmSubjectsTag : hmSubjectsTags) {
            SubjectsResult subjectsResult = new SubjectsResult();
            subjectsResult.setValue(hmSubjectsTag.getId());
            subjectsResult.setLabel(hmSubjectsTag.getTagname());
            subjectsResults.add(subjectsResult);
        }
        return subjectsResults;

    }
}
